/*
 * UserListUI.java
 *
 * Created on 27. august 2008, 17:59
 */

package jturbofile.gui;

import java.awt.GridLayout;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import jturbofile.net.PeerThread;

/**
 *
 * @author  Rolf
 */
public class UserListUI extends javax.swing.JPanel {

    private List<ConnectedUserUI> users;
    private JPanel userArea;
    
    /** Creates new form UserListUI */
    public UserListUI() {
        users = new LinkedList<ConnectedUserUI>();
        
        initComponents();
        
        userArea = new JPanel();
        this.setLayout(new GridLayout(1,1));
        this.add(new JScrollPane(userArea));
        
        updateUsers();
    }

    public void reset() {
        userArea.removeAll();
        users.clear();
    }
    
    public void addUser(PeerThread pt) {
        users.add(new ConnectedUserUI(pt));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    public void updateUsers() {
        userArea.setLayout(new GridLayout(users.size(), 1));
        
        for(ConnectedUserUI user : users) {
            userArea.add(user);
        }
        
        userArea.invalidate();
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
